package com.acwing;

import com.leetcode.ListNode;

/**
 * 输入一个链表，输出该链表中倒数第 k 个结点
 */
public class No33 {
    public ListNode findKthToTail(ListNode pListHead, int k) {
        if (pListHead == null) {
            return null;
        }
        ListNode node = pListHead;
        int num = 0;
        while (node != null) {
            num++;
            node = node.next;
        }
        if (k > num) {
            return null;
        }
        int step = num - k;
        node = pListHead;
        while (step-- > 0) {
            node = node.next;
        }
        return node;
    }
}
